package com.example.stuwork.controller;

import com.example.stuwork.common.Result;
import com.example.stuwork.pojo.ClassCourse;
import com.example.stuwork.pojo.Course;
import com.example.stuwork.pojo.Student;
import com.example.stuwork.service.AssignmentAllocationService;
import com.example.stuwork.service.ClassCourseService;
import com.example.stuwork.service.CourseService;
import com.example.stuwork.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * 学生课程控制器
 * 处理学生查询自己所属班级课程的请求
 */
@RestController
@RequestMapping("/student-course")
public class StudentCourseController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private ClassCourseService classCourseService;
    
    @Autowired
    private CourseService courseService;
    
    @Autowired
    private AssignmentAllocationService assignmentAllocationService;

    /**
     * 获取学生所属班级的所有课程
     *
     * @param studentId 学生ID
     * @return 课程列表
     */
    @GetMapping("/list/{studentId}")
    public Result<List<ClassCourse>> getStudentCourses(@PathVariable String studentId) {
        try {
            // 1. 通过学生ID获取学生信息
            Result<Student> studentResult = studentService.getStudentById(studentId);
            if (!studentResult.isSuccess()) {
                return Result.error(studentResult.getMessage());
            }

            Student student = studentResult.getData();
            if (student == null) {
                return Result.error("学生信息不存在");
            }

            // 2. 获取学生所属班级ID
            Long classId = student.getClassId();
            if (classId == null) {
                return Result.error("学生未分配班级");
            }

            // 3. 根据班级ID获取班级课程列表
            List<ClassCourse> classCourses = classCourseService.findByClassId(classId);
            
            return Result.success("获取学生课程成功", classCourses);
        } catch (Exception e) {
            return Result.error("获取学生课程失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取课程详情
     *
     * @param courseId 课程ID
     * @param classId 班级ID
     * @return 课程详情
     */
    @GetMapping("/detail")
    public Result<Map<String, Object>> getCourseDetail(
            @RequestParam String courseId,
            @RequestParam Long classId,
            @RequestParam String studentId) {
        try {
            // 1. 获取课程基本信息
            Result<Course> courseResult = courseService.getCourseById(courseId);
            if (!courseResult.isSuccess()) {
                return Result.error(courseResult.getMessage());
            }
            Course course = courseResult.getData();
            
            // 2. 获取班级课程关联信息
            ClassCourse classCourse = classCourseService.findByCourseIdAndClassId(courseId, classId);
            if (classCourse == null) {
                return Result.error("该班级未分配此课程");
            }
            
            // 3. 同步该学生的作业分配（按需同步）
            assignmentAllocationService.syncAssignmentsForNewStudent(studentId, classId);
            
            // 4. 整合课程信息
            Map<String, Object> courseDetail = new HashMap<>();
            courseDetail.put("courseInfo", course);
            courseDetail.put("classInfo", classCourse);
            
            return Result.success("获取课程详情成功", courseDetail);
        } catch (Exception e) {
            return Result.error("获取课程详情失败：" + e.getMessage());
        }
    }
} 